package com.chen;

import org.quartz.*;
import org.quartz.impl.StdSchedulerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;

public class Demo{

        public static void main(String[] args) throws SchedulerException, InterruptedException {


            // 1、创建JobDetail实例，并与PrintWordsJob类绑定(Job执行内容)

            JobDetail jobDetail = JobBuilder.newJob(PrintWordsJob.class)

                    .withIdentity("job1", "group1").build();


            // 2、构建Trigger实例,每隔1s执行一次

            Trigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggerGroup1")

                    .startNow()// 立即生效

                    .withSchedule(SimpleScheduleBuilder.simpleSchedule()

                            .withIntervalInSeconds(1)// 每隔1s执行一次

                            .repeatForever()).build();// 一直执行


            // 3、创建调度器Scheduler并执行

            Scheduler scheduler = new StdSchedulerFactory().getScheduler();

            scheduler.scheduleJob(jobDetail, trigger);

            System.out.println("--------scheduler start ! ------------");

            System.out.println("at:" + new SimpleDateFormat("yy-MM-dd HH-mm-ss:SSS").format(new Date()) + ", prints: Hello scheduler");

            scheduler.start();


            // 睡眠12秒

            TimeUnit.MILLISECONDS.sleep(12000);

            scheduler.shutdown();

            System.out.println("at:" + new SimpleDateFormat("yy-MM-dd HH-mm-ss:SSS").format(new Date()) + ", prints: Hello scheduler");

            System.out.println("--------scheduler shutdown ! ------------");

        }

}